local _G = GLOBAL
local containers = require("containers")
local params = {}
local cooking = require("cooking")

--兼容模组"Show Me (中文)"的容器列表
local showmeneed = {
    "tf_icebox_chillet",
    "tf_beverage_cabinet",
}

if TUNING.TF_C_N == "more" then
    --================================
    --[[注册容器:疾旋鼬冰箱]]
    --================================
    params.tf_icebox_chillet =
    {
        widget =
        {
            slotpos = {},
            animbank = "ui_boat_ancient_4x4",
            animbuild = "ui_boat_ancient_4x4",
            pos = Vector3(0, 200, 0),
            side_align_tip = 160,
        },
        type = "chest",
    }

    --设置格子数量
    for y = 3, 0, -1 do
        for x = 0, 3 do
            table.insert(params.tf_icebox_chillet.widget.slotpos, Vector3(80 * x - 80 * 2.5 + 80, 80 * y - 80 * 2.5 + 80, 0))
        end
    end

    function params.tf_icebox_chillet.itemtestfn(container, item, slot)
        if item:HasTag("icebox_valid") then
            return true
        end

        if cooking.IsCookingIngredient(item.prefab) then --只要是烹饪食材，就能放入，参考棱镜的月藏宝匣
            return true
        end

        if item:HasTag("smallcreature") then
            return true
        end

        for k, v in pairs(FOODTYPE) do
            if item:HasTag("edible_" .. v) then
                return true
            end
        end

        if not (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled")) then
            return false
        end

        return false
    end

    --================================
    --[[注册容器:冰柜]]
    --================================
    params.tf_beverage_cabinet =
    {
        widget =
        {
            slotpos = {},
            animbank = "ui_beverage_cabinet_5x5",
            animbuild = "ui_beverage_cabinet_5x5",
            pos = Vector3(0, 220, 0),
            side_align_tip = 160,
        },
        type = "chest",
    }

    --设置格子数量
    for y = 3, -1, -1 do
        for x = -1, 3 do
            table.insert(params.tf_beverage_cabinet.widget.slotpos, Vector3(75 * x - 75 * 2 + 75, 75 * y - 75 * 2 + 75, 0))
        end
    end

    function params.tf_beverage_cabinet.itemtestfn(container, item, slot)
        if item:HasTag("icebox_valid") then
            return true
        end

        if cooking.IsCookingIngredient(item.prefab) then --只要是烹饪食材，就能放入，参考棱镜的月藏宝匣
            return true
        end

        if item:HasTag("smallcreature") then
            return true
        end

        for k, v in pairs(FOODTYPE) do
            if item:HasTag("edible_" .. v) then
                return true
            end
        end

        if not (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled")) then
            return false
        end

        return false
    end
else
    --================================
    --[[注册容器:疾旋鼬冰箱]]
    --================================
    params.tf_icebox_chillet =
    {
        widget =
        {
            slotpos = {},
            animbank = "ui_chest_3x3",
            animbuild = "ui_chest_3x3",
            pos = Vector3(0, 200, 0),
            side_align_tip = 160,
        },
        type = "chest",
    }

    --设置格子数量
    for y = 2, 0, -1 do
        for x = 0, 2 do
            table.insert(params.tf_icebox_chillet.widget.slotpos, Vector3(80 * x - 80 * 2 + 80, 80 * y - 80 * 2 + 80, 0))
        end
    end

    function params.tf_icebox_chillet.itemtestfn(container, item, slot)
        if item:HasTag("icebox_valid") then
            return true
        end

        if cooking.IsCookingIngredient(item.prefab) then --只要是烹饪食材，就能放入，参考棱镜的月藏宝匣
            return true
        end

        if item:HasTag("smallcreature") then
            return true
        end

        for k, v in pairs(FOODTYPE) do
            if item:HasTag("edible_" .. v) then
                return true
            end
        end

        if not (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled")) then
            return false
        end

        return false
    end

    --================================
    --[[注册容器:冰柜]]
    --================================
    params.tf_beverage_cabinet =
    {
        widget =
        {
            slotpos = {},
            animbank = "ui_chest_3x3",
            animbuild = "ui_chest_3x3",
            pos = Vector3(0, 200, 0),
            side_align_tip = 160,
        },
        type = "chest",
    }

    --设置格子数量
    for y = 2, 0, -1 do
        for x = 0, 2 do
            table.insert(params.tf_beverage_cabinet.widget.slotpos, Vector3(80 * x - 80 * 2 + 80, 80 * y - 80 * 2 + 80, 0))
        end
    end

    function params.tf_beverage_cabinet.itemtestfn(container, item, slot)
        if item:HasTag("icebox_valid") then
            return true
        end

        if cooking.IsCookingIngredient(item.prefab) then --只要是烹饪食材，就能放入，参考棱镜的月藏宝匣
            return true
        end

        if item:HasTag("smallcreature") then
            return true
        end

        for k, v in pairs(FOODTYPE) do
            if item:HasTag("edible_" .. v) then
                return true
            end
        end

        if not (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled")) then
            return false
        end

        return false
    end
end

--================================
--[[注册容器:火锅架子]]
--================================
params.tf_hot_pot_rack =
{
    widget =
    {
        slotpos            = {},
        slotbg             = {},
        animbank           = "ui_tf_hot_pot_rack_3x3",
        animbuild          = "ui_tf_hot_pot_rack_3x3",
        animbank_upgraded  = "ui_tf_hot_pot_rack_3x3",
        animbuild_upgraded = "ui_tf_hot_pot_rack_3x3",
        pos                = Vector3(0, 200, 0),
        side_align_tip     = 160,
    },
    type = "chest",
}

local hot_pot_rack_bg = { image = "tf_hot_pot_rack_slot.tex", atlas = "images/tf_hud.xml" }

for y = 2, 0, -1 do
    for x = 0, 2 do
        table.insert(params.tf_hot_pot_rack.widget.slotpos, Vector3(80 * x - 80 * 2 + 80, 80 * y - 80 * 2 + 80, 0))
        table.insert(params.tf_hot_pot_rack.widget.slotbg, hot_pot_rack_bg)
    end
end

function params.tf_hot_pot_rack.itemtestfn(container, item, slot)
    if item:HasTag("icebox_valid") then
        return true
    end

    if cooking.IsCookingIngredient(item.prefab) then -- 只要是烹饪食材，就能放入，参考棱镜的月藏宝匣
        return true
    end

    if item:HasTag("smallcreature") then
        return true
    end

    for k, v in pairs(FOODTYPE) do
        if item:HasTag("edible_" .. v) then
            return true
        end
    end

    if not (item:HasTag("fresh") or item:HasTag("stale") or item:HasTag("spoiled")) then
        return false
    end

    return false
end

--================================
--[[注册容器:炊香台]]
--================================
params.tf_cookpot =
{
    widget =
    {
        slotpos =
        {
            Vector3(0, 64 + 32 + 8 + 4, 0),
            Vector3(0, 32 + 4, 0),
            Vector3(0, -(32 + 4), 0),
            Vector3(0, -(64 + 32 + 8 + 4), 0),
        },
        animbank = "ui_cookpot_1x4",
        animbuild = "ui_cookpot_1x4",
        pos = Vector3(200, 0, 0),
        side_align_tip = 100,
        buttoninfo =
        {
            text = STRINGS.ACTIONS.COOK,
            position = Vector3(0, -165, 0),
        }
    },
    acceptsstacks = false,
    type = "cooker",
}

function params.tf_cookpot.itemtestfn(container, item, slot)
    return cooking.IsCookingIngredient(item.prefab) and not container.inst:HasTag("burnt")
end

function params.tf_cookpot.widget.buttoninfo.fn(inst, doer)
    if inst.components.container ~= nil then
        BufferedAction(doer, inst, ACTIONS.COOK):Do()
    elseif inst.replica.container ~= nil and not inst.replica.container:IsBusy() then
        SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.COOK.code, inst, ACTIONS.COOK.mod_name)
    end
end

-- 烹饪按钮只有容器被塞满才可以点击
function params.tf_cookpot.widget.buttoninfo.validfn(inst)
    return inst.replica.container ~= nil and inst.replica.container:IsFull()
end

--================================
--[[修改容器注册函数]]
--================================
for k, v in pairs(params) do
    containers.params[k] = v

    --更新容器格子数量的最大值
    containers.MAXITEMSLOTS = math.max(containers.MAXITEMSLOTS, v.widget.slotpos ~= nil and #v.widget.slotpos or 0)
end
params = nil --释放内存

--================================
--[[mod兼容：Show Me (中文)]]
--================================
--showme优先级如果比本mod高，那么这部分代码会生效
for k, mod in pairs(ModManager.mods) do
    if mod and _G.rawget(mod, "SHOWME_STRINGS") then --showme特有的全局变量
        if
            mod.postinitfns and mod.postinitfns.PrefabPostInit and
            mod.postinitfns.PrefabPostInit.treasurechest
        then
            for _, v in ipairs(showmeneed) do
                mod.postinitfns.PrefabPostInit[v] = mod.postinitfns.PrefabPostInit.treasurechest
            end
        end
        break
    end
end

--showme优先级如果比本mod低，那么这部分代码会生效
TUNING.MONITOR_CHESTS = TUNING.MONITOR_CHESTS or {}
for _, v in ipairs(showmeneed) do
    TUNING.MONITOR_CHESTS[v] = true
end
